<script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n'
import { ElImage, ElButton } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { getProcessProductListApi } from '@/api/product'
import { ProductData } from '@/api/product/types'
import { TableColumn } from '@/types/table'
import { useValidator } from '@/hooks/web/useValidator'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import {ref, reactive } from 'vue'
import { FormSchema } from '@/types/form'
import { Table } from '@/components/Table'
import { useTable } from '@/views/Product/useTable'
import Write from "@/views/Example/Dialog/components/Write.vue";


const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'productName',
    label: '商品名称',
    component: 'Input'
  },
  {
    field: 'categroyName',
    label: '商品分类',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: 'java基础-书籍',
        },
       ]
    }
  },
  {
    field: 'trueName',
    label: '上传人员',
    component: 'Input'
  }
])

const columns = reactive<TableColumn[]>([
  {
    field: 'product_id',
    label: '序号'
  },
  {
    field: 'product_code',
    label: t('productTable.productCode')
  },
  {
    field: 'main_image',
    label: t('productTable.mainImage')
  },
  {
    field: 'product_name',
    label: t('productTable.productName'),
    search: {
      show: true
    }
  },
  {
    field: 'currentprice_product',
    label: t('productTable.currentPriceProduct')
  },
  {
    field: 'categroy_name',
    label: '商品分类',
    search: {
      show: true
    }
  },
  {
    field: 'true_name',
    label: '上传人员',
    search: {
      show: true
    }
  },
  {
    field: 'product_sate',
    label: '审核状态',
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      if(cellValue == 2){
        return "待审核"
      }else if(cellValue == 3){
        return "审核通过"
      }else if(cellValue == 4){
        return "未通过"
      }
    }
  },
  {
    field: 'action',
    width: '200px',
    label: t('tableDemo.action')
  }
])

const dialogVisible = ref(false)

const dialogTitle = ref('')

//获取個人商品數據
const { register, tableObject, methods } = useTable<ProductData>({
  getListApi: getProcessProductListApi,
  response: {
    list: 'list',
    total: 'total'
  },
  props: {
    columns
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { push } = useRouter()

const action = (row: ProductData, type: number) => {
  push(`/product/process-${type}?product_id=${row.product_id}`)
}
</script>

<template>
  <ContentWrap :title="t('searchDemo.search')">
    <Search :schema="schema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>
  <ContentWrap :title="t('productTable.personal')">
    <Table
      :border="true"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :data="tableObject.tableList.slice((tableObject.currentPage-1)*tableObject.pageSize,tableObject.currentPage*tableObject.pageSize)"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <span v-if="row.product_sate == 3">
          <ElButton type="success" @click="action(row, 'chakan')">
            {{ t('productTable.chakan') }}
          </ElButton>
        </span>
        <span v-if="row.product_sate == 2">
          <ElButton type="success" @click="action(row, 'chakan')">
            {{ t('productTable.chakan') }}
          </ElButton>
          <ElButton type="warning" @click="action(row, 'shenhe')">
            {{ t('productTable.shenhe') }}
          </ElButton>
        </span>
        <span v-if="row.product_sate == 4">
          <ElButton type="success" @click="action(row, 'chakan')">
            {{ t('productTable.chakan') }}
          </ElButton>
        </span>
      </template>
      <!--商品图片-->
      <template #main_image="{ row }:{ row: ProductData }">
        <ElImage
            style="width: 60px;height: 60px;"
            :src="row.main_image"
            :preview-src-list="[row.main_image]"
            hide-on-click-modal="true"
            preview-teleported="true"
        />
      </template>
    </Table>
  </ContentWrap>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Write
        v-if="actionType === 'shenhe'"
        ref="writeRef"
        :form-schema="allSchemas.formSchema"
        :current-row="tableObject.currentRow"
    />
  </Dialog>
</template>
